package com.lancoo.klgcourseware.ui.adapter;

import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.MoreSlideMenuModel;
import com.lancoo.klgcourseware.listener.OnItemClickListener;

import java.util.List;

public class MoreMenuAdapter extends RecyclerView.Adapter<MoreMenuAdapter.MoreMenuViewHolder>{

    private List<MoreSlideMenuModel> mModuleDetails;

    private OnItemClickListener mOnItemClickListener;

    public MoreMenuAdapter(List<MoreSlideMenuModel> moreModuleDetails,OnItemClickListener onItemClickListener){
        mModuleDetails = moreModuleDetails;
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public MoreMenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.klg_item_more_menu,parent,false);

        final MoreMenuViewHolder viewHolder =new MoreMenuViewHolder(view);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position =viewHolder.getAdapterPosition();
                if (mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MoreMenuViewHolder holder, int position) {
        holder.bind(mModuleDetails.get(position));
    }

    @Override
    public int getItemCount() {
        return mModuleDetails.size();
    }

    static class MoreMenuViewHolder extends RecyclerView.ViewHolder{

        private TextView mTvModuleName;

        private View mLine;

        private ImageView mImgPass;

        MoreMenuViewHolder(View view){
            super(view);
            mTvModuleName = view.findViewById(R.id.tv_module_name);
            mLine = view.findViewById(R.id.line);
            mImgPass = view.findViewById(R.id.img_pass);
        }

        private void bind(MoreSlideMenuModel moreModuleDetail){
            mTvModuleName.setText(moreModuleDetail.getModuleName());
            mImgPass.setVisibility(moreModuleDetail.getIsPass()==1?View.VISIBLE:View.GONE);

            mTvModuleName.setTextColor(moreModuleDetail.getSelected()==1? Color.WHITE:Color.parseColor("#666666"));
            mTvModuleName.setBackgroundColor(moreModuleDetail.getSelected()==1? Color.parseColor("#19AEF8"):Color.TRANSPARENT);
            mLine.setBackgroundColor(moreModuleDetail.getSelected()==1? Color.parseColor("#19AEF8"):Color.WHITE);
            mImgPass.setImageResource(moreModuleDetail.getSelected()==1?R.mipmap.klg_icon_pass_white:R.mipmap.klg_icon_pass_blue);
        }
    }

}
